package HomeWork1;

import HomeWork3.Store;

public class test {
    public static void main(String[] args) {
        Store store=new Store();
        Add a1=new Add(store);
        Add a2=new Add(store);

        Reduction r1=new Reduction(store);
        Reduction r2=new Reduction(store);

        new Thread(a1).start();
        new Thread(a2).start();
        new Thread(r1).start();
        new Thread(r2).start();
    }

    static class Store {
        private int size=0;

        public synchronized void add() throws InterruptedException {
            this.size++;
            System.out.println("当前值"+this.size);
            Thread.sleep(100);
        }

        public synchronized void reduction() throws InterruptedException {
            this.size--;
            System.out.println("当前值"+this.size);
            Thread.sleep(100);
        }
    }

     static class Add implements Runnable{
         private Store store;

         public Add(Store store) {
             this.store = store;
         }
        @Override
        public void run() {
            while (true){
                try {
                    store.add();
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    static class Reduction implements Runnable{
        private Store store;

        public Reduction(Store store) {
            this.store = store;
        }
        @Override
        public void run() {
            try {
                store.reduction();
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
